/*
 * gs_button.c
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>

#include "gs_table.h"

typedef struct _GS_TABLEHEADER
{
    char* text;
    int width;
}GS_TABLEHEADER;
typedef GS_TABLEHEADER* PGS_TABLEHEADER;

typedef struct _GS_TABLEDATA
{
    int rows;
    int cols;
    int def_width;
    int def_height;
    GS_TABLEHEADER* head;
    char** data;
} GS_TABLEDATA;
typedef GS_TABLEDATA* PGS_TABLEDATA;

static void draw_table (HWND hWnd, HDC hdc, PGS_TABLEDATA pData)
{
    int i;
    int rows, h;
    int old_mode;
    RECT rect;
    gal_pixel old_pixel;
    
    GetClientRect (hWnd, &rect);

    old_mode = SetBkMode (hdc, BM_TRANSPARENT);
    old_pixel = SetBrushColor (hdc, RGB2Pixel(hdc, 255, 255, 210));

    h = pData->def_height;
    rows = RECTH(rect) / h + 1;

    // Draw backgroup shape
    for (i = 0; i < rows; i++) {
        if (i % 2) {
            FillBox (hdc, rect.left, i * h, RECTW(rect), h);
        }
        
    }

    // Draw header content
    for (i=0; i < pData->cols; i++) {
        RECT r;
        
        r.left = i*pData->def_width; //?????????????????       
        r.right = r.left + pData->def_width;
        r.top = 0;
        r.bottom = h;
        DrawText (hdc, pData->head[i].text, -1, &r, DT_SINGLELINE | DT_LEFT | DT_VCENTER);
    }

    SetBrushColor (hdc, old_pixel);
    old_mode = SetBkMode (hdc, old_mode);    
}

static int GsTableProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    PGS_TABLEDATA pData = NULL;
    
    if (message != MSG_CREATE)
        pData = (PGS_TABLEDATA) GetWindowAdditionalData2 (hWnd);
            
    switch (message) 
    {
        case MSG_CREATE:
        {
            GS_TABLEVIEW* data;
            
            pData = (PGS_TABLEDATA) calloc (1, sizeof (GS_TABLEDATA));
            
            if (pData == NULL) 
                return -1;

            data = (GS_TABLEVIEW*) GetWindowAdditionalData(hWnd);
            
            if (data) {
                int i;
                pData->rows = data->nr_rows;
                pData->cols = data->nr_cols;
                pData->def_width = data->col_width;
                pData->def_height = data->row_height;
                pData->head = (PGS_TABLEHEADER) calloc (data->nr_cols, sizeof(GS_TABLEHEADER));
                for (i = 0; i < pData->cols; i++) {
                    pData->head[i].text = NULL;
                    pData->head[i].width = pData->def_width;
                }
                pData->data = (char **) calloc (data->nr_cols * data->nr_rows, sizeof(char *));
            }
            else {
                return -1;
            }
            
            SetWindowAdditionalData2 (hWnd, (DWORD)pData);

            break; 
        }
        case MSG_DESTROY:
            if (pData->head)
                free (pData->head);
            
            if (pData->data) {
                int i;
                for (i = pData->rows * pData->cols; i > 0; i--) {
                    if (pData->data[i])
                        free (pData->data[i]);
                }
                free (pData->data);
            }
            
            if (pData)
                free (pData);
            
            break;

        case MSG_PAINT:
        {
            HDC hdc;
            hdc = BeginPaint(hWnd);
            
            draw_table (hWnd, hdc, pData);

            EndPaint (hWnd, hdc);
            break;
        }

        case GS_TBLM_SETHEADER:
            {
                int index = (int) wParam;
                char *text = (char*) lParam;
                GS_TABLEHEADER* head;
                
                if (!text)
                    return -1;
                else
                    text = strdup (text);
               
                if (index < 0 || index > (pData->cols - 1))
                    return FALSE;
                
                head = &pData->head[index];
                
                head->text = text;

                InvalidateRect (hWnd, NULL, TRUE);
                
                break;
            }

        case GS_TBLM_SETCELL:

            break;
            
        case GS_TBLM_SETCOLWIDTH:

            break;

        case GS_TBLM_SETROWHEIGHT:

            break;
    }
    
    return DefaultControlProc (hWnd, message, wParam, lParam);  
}

BOOL RegisterGsTable (void)
{
    WNDCLASS WndClass;

    WndClass.spClassName = GS_CTRL_TABLE;
    WndClass.dwStyle     = WS_NONE;
    WndClass.dwExStyle   = WS_EX_NONE;
    WndClass.hCursor     = GetSystemCursor (IDC_ARROW);
    WndClass.iBkColor    = RGB2Pixel (HDC_SCREEN, 200, 200, 200); 
    WndClass.WinProc     = GsTableProc;

    return RegisterWindowClass (&WndClass);
}

void UnregisterGsTable (void)
{
    UnregisterWindowClass (GS_CTRL_TABLE);
}


